package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.pms.entity.YytPmsSpuComment;
import com.ruoyi.common.pms.service.YytPmsSpuCommentService;
import com.ruoyi.utils.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品评价Controller
 *
 * @author tanxingxian
 * @date 2022-01-18 10:19
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private YytPmsSpuCommentService yytPmsSpuCommentService;

    /**
     * 根据skuId获取评价列表
     */
    @GetMapping("/commentPage")
    public ResponseResult commentPage(@RequestParam("page") int page, @RequestParam("size") int size, @RequestParam("skuId") String skuId,
                                      @RequestParam(value = "spuId", required = false) String spuId) {
        LambdaQueryWrapper<YytPmsSpuComment> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(spuId)) {    // 如果spuId不为空那就是查询该spu的评价
            wrapper.eq(YytPmsSpuComment::getSpuId, spuId);
        } else {
            wrapper.eq(YytPmsSpuComment::getSkuId, skuId);
        }
        wrapper.eq(YytPmsSpuComment::getCommentType, 0);    // 对商品的评价
        wrapper.orderByDesc(YytPmsSpuComment::getCreateTime);
        return ResponseResult.success(yytPmsSpuCommentService.page(new Page<>(page, size), wrapper));
    }

    /**
     * 根据commentId获取子评论列表
     */
    @GetMapping("/getSubComments")
    public ResponseResult getSubComments(@RequestParam("commentId") String commentId) {
        LambdaQueryWrapper<YytPmsSpuComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YytPmsSpuComment::getParentId, commentId);
        wrapper.eq(YytPmsSpuComment::getCommentType, 1);
        wrapper.orderByDesc(YytPmsSpuComment::getCreateTime);
        return ResponseResult.success(yytPmsSpuCommentService.list(wrapper));
    }

    /**
     * 根据id获取评价信息对象
     */
    @GetMapping("/getDetailInfo")
    public ResponseResult getDetailInfo(@RequestParam("id") String id) {
        return ResponseResult.success(yytPmsSpuCommentService.getById(id));
    }

}
